package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.pojo.Rights;
import net.bytebuddy.matcher.CollectionItemMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘昱江
 * 时间 2021/2/18
 */
@Service
public class RightsServiceImpl implements RightsService {

    @Autowired
    private RightsMapper rightsMapper;

    @Override
    public List<Rights> getRightsList() {

        return rightsMapper.getRightsList();
    }

    @Override
    public List<Rights> getRightsListAll() {
        //封装权限列表数据 查询所有的一级菜单
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<Rights> oneRightsList = rightsMapper.selectList(queryWrapper);
        //根据一级查询二级
        for(Rights oneRights : oneRightsList){
            int id = oneRights.getId();
            //将Id当作parentId查询子级数据信息
            List<Rights> twoRightsList = findChildrenList(id);
            //根据二级查询3级菜单
            for(Rights twoRights : twoRightsList){
                int twoId = twoRights.getId();
                List<Rights> threeRightsList = findChildrenList(twoId);
                //将三级数据进行保存
                twoRights.setChildren(threeRightsList);
            }
            oneRights.setChildren(twoRightsList);
        }
        return oneRightsList;
    }

    public List<Rights> findChildrenList(Integer parentId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", parentId);
        return rightsMapper.selectList(queryWrapper);
    }

    @Override
    public Rights getGightsName(Integer id) {

        return rightsMapper.selectById(id);
    }



    @Override
    public List<Rights> getRightsListByIds(List<Integer> rightsIds) {
        //1.获取所有的一级菜单
        List<Rights> oneRightsList = findRightsListByLevelAndIds(0,1,rightsIds);

        //2.封装二级查询菜单
        for (Rights oneRights : oneRightsList){
            List<Rights> twoRightsList = findRightsListByLevelAndIds(oneRights.getId(),2,rightsIds);

            //3.封装3级菜单
            for(Rights twoRights :twoRightsList){
                List<Rights> threeRightsList = findRightsListByLevelAndIds(twoRights.getId(),3,rightsIds);
                twoRights.setChildren(threeRightsList);
            }
            oneRights.setChildren(twoRightsList);
        }
        return oneRightsList;
    }

    @Override
    public List<Integer> getRightsChildrenList(Integer rightsId) {
        List<Integer> rightsIdList = new ArrayList<>();
        List<Rights> childrenList = findChildrenList(rightsId);
        for (Rights rights : childrenList){
            rightsIdList.add(rights.getId());
        }
        return rightsIdList;
    }

    @Override
    public List<Integer> getThreeRightsIds(Integer roleId) {

        return rightsMapper.getThreeRightsIds(roleId);
    }


    public List<Rights> findRightsListByLevelAndIds(Integer parentId,Integer level,List<Integer> ids){
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", level)
                    .eq("parent_id", parentId)
                    .in("id",ids);
        return rightsMapper.selectList(queryWrapper);
    }

}
